package org.gc.amino.ia;

import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.gc.amino.engine.Game;
import org.gc.amino.engine.mote.Mote;
import org.gc.amino.engine.terrainmap.PointD;
import org.gc.amino.ia.httpserver.IaDeliveryInterface;
import org.gc.amino.ia.httpserver.IaLauncher;

/**
 * This IA goes erratically towards nonsense directions.
 * It is only a simple proof of concept of an IA. 
 */
public class StupidIA2 implements IaDeliveryInterface {

    private static final Logger log = Logger.getLogger( StupidIA2.class );

    private Random random = new Random();
    private PointD terrainSize;
    private PointD currentDirection = null;
    private int moving_counter = 0;
    
    @Override
    public void init( PointD size ) {
        terrainSize = size;
    }
    
    @Override
    public PointD frame( Mote you, List<Mote> otherMotes ) {
        PointD bestDirection = new PointD(0,0);
        double bestScore = 3;

    	if ( moving_counter > 0 ) {
            moving_counter--;
            return new PointD(0,0);
        } else{
            currentDirection=new PointD(0,0);
            moving_counter = 5;
        	for(int x=-1;x<2;x++){
        		for(int y=-1;y<2;y++){
        			currentDirection = new PointD(x,y);
	                PointD dest = new PointD(2*you.getPosition().x -currentDirection.x, 2*you.getPosition().y-currentDirection.y);
	                if(simulateAndGetHeuristic(you, otherMotes, dest, 5)> bestScore){
	                	bestDirection = currentDirection;
	                	bestScore = simulateAndGetHeuristic(you, otherMotes, dest, 50);
	                }
        		}
        	}
            currentDirection = bestDirection;
            log.info( "New current direction " + currentDirection ); 
            return currentDirection;
        } 
        //else return null;
    }

    
    
    private double simulateAndGetHeuristic(Mote you, List<Mote> otherMotes, PointD pos, int nbRound){
    	double score, prevRadius = you.getRadius();
    	
    	//Copy
    	List<Mote> clonedOtherMotes = new ArrayList<Mote>();
    	Game.clearMotes();
    	Mote youCloned = you.copy();
    	Game.addMote(youCloned);
    	for (Mote m : otherMotes){
    		Mote mCloned = m.copy();
    		clonedOtherMotes.add(m);
    		Game.addMote(mCloned);
    	}
    	
    	//Simulate
    	youCloned.move(pos);
    	for (int i=0 ;i< nbRound;i++){
    		youCloned.update();
    		for(Mote m : clonedOtherMotes){
    			m.update();
    		}
    	}
    	score=getHeuristicValue(you, otherMotes, prevRadius);
    	
    	//Reset
    	Game.clearMotes();
    	Game.addMote(you);
    	for (Mote m : otherMotes){
    		Game.addMote(m);
    	}
    	return score;
    }

    private double getHeuristicValue(Mote you, List<Mote>otherMotes, double prevRadius){
    	double v = 0;
    	System.out.println(-prevRadius+you.getRadius()/prevRadius);
    	return (-prevRadius+you.getRadius()/prevRadius);
    	/*
    	for(Mote m : otherMotes){
    		if(prevRadius*0.9>you.getRadius()) return -99999;
    		if(! m.isDead())
    		{
    			
    			v+=(you.getRadius()-m.getRadius())/(you.getDistance(m)-m.getRadius()-you.getRadius());
    			if(m.getRadius() < you.getRadius())
    				v+=2*(you.getRadius()-m.getRadius())/(you.getDistance(m)-m.getRadius()-you.getRadius());
    			
    			
    			if(m.getRadius() < you.getRadius())
    				v+= ((float)(you.getRadius()-m.getRadius()))/(you.getDistance(m)-m.getRadius()-you.getRadius());
    			else 
    				if (you.getDistance(m)-m.getRadius()-you.getRadius()<100) return -99999;
    				else v-=2*((float)(you.getRadius()-m.getRadius()))/(you.getDistance(m)-m.getRadius()-you.getRadius());
    			
    		}
    	}
    	return v;*/
    }
    
    
    public static void main( String[] argv ) throws IOException {
        IaLauncher.launch( argv, new StupidIA2() , 1235);
    }

}
